# -*- coding: utf-8 -*-

from flask import jsonify, request

from apps import log
from apps import result
from apps.common import CodeException


class ParameterException(CodeException):
    """
    参数错误
    """

    def __init__(self, name, value=None):
        super(ParameterException, self).__init__(1, 'parameter error [%s:%s]' % (name, value))


class SQLException(CodeException):
    """
    参数错误
    """

    def __init__(self):
        super(SQLException, self).__init__(1, 'sql error')


def handler(exc):
    if request and hasattr(request, 'storage_context'):
        delattr(request, 'storage_context')
    log.warning('code exception: %s' % exc)
    return jsonify(result.from_exc(exc))
